import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:oktoast/oktoast.dart';
import 'package:yurun_smartmerchants/generated/json/base/json_convert_content.dart';
import 'package:yurun_smartmerchants/models/add_staff_entity.dart';
import 'package:yurun_smartmerchants/net/ApiInterface.dart';
import 'package:yurun_smartmerchants/net/NetUtil.dart';
import 'package:yurun_smartmerchants/page/verify_code.dart';
import 'package:yurun_smartmerchants/res/colours.dart';
import 'package:yurun_smartmerchants/utils/app_size.dart';
import 'package:yurun_smartmerchants/utils/evenbus_utils.dart';
import 'package:yurun_smartmerchants/utils/loading_utils.dart';

class AddStaffPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _AddStaffPageState();
  }
}

class _AddStaffPageState extends State<AddStaffPage> {
  var nameController = TextEditingController();
  var phoneController = TextEditingController();
  var codeController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colours.white,
        centerTitle: true,
        iconTheme: IconThemeData(color: Colours.app_words),
        title: Text(
          '新增员工',
          style: TextStyle(fontSize: 18, color: Colours.app_words),
        ),
      ),
      resizeToAvoidBottomPadding: false,
      body: Container(
        color: Colours.app_background,
        child: Column(
          children: <Widget>[
            Expanded(
                child: Container(
                  child: Column(
                    children: <Widget>[
                      Container(
                        color: Colors.white,
                        margin: EdgeInsets.only(top: AppSize.height(20)),
                        height: AppSize.height(98),
                        child: Row(
                          children: <Widget>[
                            Expanded(
                                flex: 1,
                                child: Container(
                                  margin: EdgeInsets.only(left: AppSize.width(32)),
                                  child: Text('员工姓名',style: TextStyle(fontSize: AppSize.sp(32),color: Colours.color_313131),),
        )),
                            Expanded(
                                flex: 1,
                                child: Container(
                                  margin: EdgeInsets.only(right: AppSize.width(32)),
                                  child: TextField(
                                    controller: nameController,
                                    textAlign: TextAlign.right,
                                    maxLength: 4,
                                    style: TextStyle(fontSize: AppSize.sp(32),color: Colours.color_313131),
                                    keyboardType: TextInputType.text,
                                    decoration: InputDecoration(
                                      counterText: '',
                                      border: InputBorder.none,
                                      hintText: '输入姓名',
                                      hintStyle: TextStyle(fontSize: AppSize.sp(32),color: Colours.color_abacb5),
                                    ),
                                  ),
                                )),
                          ],
                        ),
                      ),
                      Divider(thickness: AppSize.height(1),height: AppSize.height(1),color: Colours.app_background,),
                      Container(
                        color: Colors.white,
                        height: AppSize.height(98),
                        child: Row(
                          children: <Widget>[
                            Expanded(
                                flex: 1,
                                child: Container(
                                  margin: EdgeInsets.only(left: AppSize.width(32)),
                                  child: Text('联系电话',style: TextStyle(fontSize: AppSize.sp(32),color: Colours.color_313131),),
                                )),
                            Expanded(
                                flex: 1,
                                child: Container(
                                  margin: EdgeInsets.only(right: AppSize.width(32)),
                                  child: TextField(
                                    controller: phoneController,
                                    textAlign: TextAlign.right,
                                    maxLength: 11,
                                    keyboardType: TextInputType.number,
                                    inputFormatters: [WhitelistingTextInputFormatter.digitsOnly],
                                    style: TextStyle(fontSize: AppSize.sp(32),color: Colours.color_313131),
                                    decoration: InputDecoration(
                                      counterText: '',
                                      border: InputBorder.none,
                                      hintText: '输入电话号码',
                                      hintStyle: TextStyle(fontSize: AppSize.sp(32),color: Colours.color_abacb5),
                                    ),
                                    onChanged: (value){
                                      setState(() {});
                                    },
                                  ),
                                )),
                          ],
                        ),
                      ),
                      Divider(thickness: AppSize.height(1),height: AppSize.height(1),color: Colours.app_background,),
                      Container(
                        color: Colors.white,
                        height: AppSize.height(98),
                        child: Row(
                          children: <Widget>[
                            Expanded(
                                flex: 1,
                                child: Container(
                                  margin: EdgeInsets.only(left: AppSize.width(32)),
                                  child: Text('验证码',style: TextStyle(fontSize: AppSize.sp(32),color: Colours.color_313131),),
                                )),
                            Expanded(
                                flex: 1,
                                child: Container(
                                  child: TextField(
                                    controller: codeController,
                                    textAlign: TextAlign.right,
                                    maxLength: 6,
                                    style: TextStyle(fontSize: AppSize.sp(32),color: Colours.color_313131),
                                    keyboardType: TextInputType.number,
                                    inputFormatters: [WhitelistingTextInputFormatter.digitsOnly],
                                    decoration: InputDecoration(
                                      counterText: '',
                                      border: InputBorder.none,
                                      hintText: '输入验证码',
                                      hintStyle: TextStyle(fontSize: AppSize.sp(32),color: Colours.color_abacb5),
                                    ),
                                  ),
                                )),
                            Expanded(
                                flex: 1,
                                child: Container(
                                  alignment: Alignment.centerRight,
                                  margin: EdgeInsets.only(right: AppSize.width(32)),
                                  child: ResetCodePage(
                                    phoneNum: phoneController.text,
                                  ),
                                )),
                          ],
                        ),
                      ),
                    ],
                  ),
                )
            ),
            Container(
              height: AppSize.height(135),
              color: Colors.white,
              alignment: Alignment.center,
              child: Container(
                height: AppSize.height(90),
                width: double.infinity,
                margin: EdgeInsets.only(
                    left: AppSize.width(20), right: AppSize.width(20)),
                child: RaisedButton(
                  onPressed: () {
                    String name = nameController.text;
                    String phone = phoneController.text;
                    String code = codeController.text;
                    if(name == null || name.isEmpty){
                      showToast('请输入员工姓名');
                      return;
                    }
                    if(phone == null || phone.isEmpty){
                      showToast('请输入联系电话');
                      return;
                    }
                    if(code == null || code.isEmpty){
                      showToast('请输入验证码');
                      return;
                    }
                    _addStaff(name, phone, code);
                  },
                  color: Colours.app_main,
                  child: Text(
                    '保存',
                    style: TextStyle(
                        fontSize: AppSize.sp(32), color: Colours.white),
                  ),
                ),
              ),
            )
          ],
        ),
      ),
    );
  }

  void _addStaff(name,phone,code){
    LoadingUtil.showLoading(context);
    ApiInterface.addStaff(code, phone, name).then((value){
      AddStaffEntity addStaffEntity = JsonConvert.fromJsonAsT<AddStaffEntity>(value);
      bool success = addStaffEntity.success;
      if(success){
        showToast('添加成功');
        EventBus().send('refresh_staff','');
      }
    }).whenComplete(() {
      LoadingUtil.hideLoading(context);
      Navigator.pop(context);
    });
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    nameController.dispose();
    phoneController.dispose();
    codeController.dispose();
  }
}
